import { Injectable } from '@angular/core';
import { Workbook } from 'exceljs';
import * as fs from 'file-saver';
@Injectable()
export class ExcelService {

    constructor(
    ) { }


    /**
     * excel 用法模板
     * 到时候需要删除
     *  
     */
    public exportExcelHisSolderTest(selfData) {
        let workbook = new Workbook();
        let worksheet = workbook.addWorksheet('焊錫成份-歷史資料');
        //1.表格标题 + 图片标题
        // let emptyArray15 = (new Array(15).fill(""));
        // let emptyArray7 = (new Array(7).fill(""));
        // let _table_heads = [...selfData.table_title, ...emptyArray15, ...selfData.circlePieTitle, ...emptyArray7, ...selfData.rowBarTitle];
        // let table_title_row = worksheet.addRow(_table_heads);
        // worksheet.mergeCells('A1:N1');
        // worksheet.mergeCells('Q1:V1');
        // worksheet.mergeCells('Y1:AE1');

        // worksheet.getRow(1).height = 32;
        // table_title_row.font = { bold: true, size: 18 };
        // table_title_row.eachCell(cell => {
        //     this.setCellStyleLight(cell, 'thin', false);
        // })
        // //2.表格表头
        // let table_heads_row = worksheet.addRow(selfData.table_heads);
        // table_heads_row.eachCell(cell => {
        //     this.setCellStyleLight(cell, 'thin', false);
        // })
        // //3.表格数据 body 部分
        // selfData.table_bodys.forEach(table_body => {
        //     let table_body_row = worksheet.addRow(table_body);
        //     table_body_row.eachCell(cell => {
        //         this.setCellStyleLight(cell, 'thin', false);
        //     })
        // });
        // //
        // //4. 图片拼接
        // /**
        //  * 饼图图片start
        //  */
        // let _imageBase64_circlePie = selfData.circlePieIns.getDataURL({ backgroundColor: "#15354a" })
        // let imageCirclePie = workbook.addImage({
        //     base64: _imageBase64_circlePie,
        //     extension: 'png',
        // });
        // worksheet.addImage(imageCirclePie, `Q2:V16`);
        // worksheet.mergeCells('Q2:V16');
        // /**
        // * 饼图图片end
        // */
        // /**
        //  * 横向柱状图图片start
        //  */
        // let _imageBase64_rowBar = selfData.rowBarIns.getDataURL({ backgroundColor: "#15354a" })
        // let imageRowBar = workbook.addImage({
        //     base64: _imageBase64_rowBar,
        //     extension: 'png',
        // });
        // worksheet.addImage(imageRowBar, `Y2:AE19`);
        // worksheet.mergeCells('Y2:AE19');
        // /**
        // * 横向柱状图图片end
        // */
        // //最后：.修饰完整表格
        // worksheet.getColumn(2).width = 15;
        // worksheet.getRow(2).height = 30;

        this.generateExcel(workbook, '焊锡成分检测管理');
    }

    /**
     * 原始基本方法/函數 start
     * 
     */

    //Generate Excel File with given name 產生excel
    private generateExcel(workbook: Workbook, fileName: string): void {
        workbook.xlsx.writeBuffer().then((data: ArrayBuffer) => {
            try {
                // let blob = new Blob();
                let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

                fs.saveAs(blob, fileName + "_" + new Date().getTime() + ".xlsx");
            } catch (error) {

            }
        })
    }
    private setCellStyleLight(cell, borderStyle: string = 'thin', fill: boolean = true) {
        cell.alignment = { horizontal: 'center', vertical: 'middle', wrapText: true };
        cell.border = { top: { style: borderStyle }, left: { style: borderStyle }, bottom: { style: borderStyle }, right: { style: borderStyle } };
        if (fill)
            cell.fill = {
                type: 'pattern',
                pattern: 'solid',
                fgColor: { argb: 'FFDCE6F1' },
                bgColor: { argb: 'FFFFFF00' }
            };
    }

    /**
     * 原始基本方法/函數 end
     * 
     */
}